using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;

namespace Babylon.Toolbox
{
    partial class StatesManager
    {
        int currentRasterizerKey = -1;
        CullMode currentCullMode = CullMode.CullCounterClockwiseFace;
        FillMode currentFillMode = FillMode.Solid;
        readonly Dictionary<int, RasterizerState> rasterizerStates = new Dictionary<int, RasterizerState>();

        int ComputeRasterizerKey()
        {
            return (((int)currentCullMode) << 3) + ((int)currentFillMode);
        }

        public FillMode FillMode
        {
            get { return currentFillMode; }
            set
            {
                currentFillMode = value;
            }
        }

        public CullMode CullMode
        {
            get { return currentCullMode; }
            set
            {
                currentCullMode = value;
            }
        }

        public void ApplyRasterizerState()
        {
            int key = ComputeRasterizerKey();

            if (key == currentRasterizerKey)
                return;

            RasterizerState rasterizerState;

            if (!rasterizerStates.TryGetValue(key, out rasterizerState))
            {
                rasterizerState = new RasterizerState
                                      {
                                          CullMode = currentCullMode,
                                          FillMode = currentFillMode,
                                          MultiSampleAntiAlias = true,
                                          ScissorTestEnable = true
                                      };

                rasterizerStates.Add(key, rasterizerState);
            }

            currentRasterizerKey = key;
            Device.RasterizerState = rasterizerState;
        }
    }
}
